// component/financesReviewTable/financesReviewTable.js
const app = getApp();
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        financeId: {
            type: String,
            value: ''
        },
        changeList: {
            type: Array,
            value: []
        },
        collectionList: {
            type: Array,
            value: []
        },
        allMoneyData: {
            type: Object,
            value: {}
        },
        total: {
            type: Number,
            value: 0
        },
        isMe: {
            type: Boolean,
            value: false
        },
        admin: {
            type: Number,
            value: 0
        },
        subAdmin: {
            type: Number,
            value: 0
        },
        userId: {
          type: Number,
          value: 0
      },
        dpId: {
            type: Number,
            value: 0
        },
        isMy: {
            type: Boolean,
            value: false
        },
        sign3: {
            type: String,
            value: '0' 
        },
        sale: {
          type: Number,
          value: 0
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        switchChecked: true,
        switchPayeeChecked: true,
        isChange: false,
        isPayee: false,
        fluxFee: '',
        flexDes: '',
        money: '',
        reason: '',
        fees: '',
        receipts: '',
        activeNames: [],
        active: ['0']
    },

    /**
     * 组件的方法列表
     */
    methods: {
        switchChange(e) {
            wx.showLoading();
            wx.request({
                url: `${app.globalData.baseUrl}/api/classify/updateChange`,
                method: "POST",
                data: { id: e.currentTarget.dataset.id, isDisable: e.detail ? '1' : '0' },
                header: {
                    "Content-Type": "application/json",
                    'token': wx.getStorageSync('token'),
                },
                success: (res) => {
                    wx.hideLoading();
                    if (res.data.code == 0) {
                        wx.showToast({
                            title: res.data.msg || '报错啦！',
                            duration: 2000
                        })
                        this.triggerEvent('addChangeCommit');
                    } else if (res.data.code == 500) {
                        wx.showToast({
                            title: res.data.msg || '报错啦！',
                            icon: 'error',
                            duration: 2000
                        })
                    } else {
                        wx.showToast({
                            title: '系统错误，请重新登录',
                        })
                    }
                },
                fail: () => {
                    wx.hideLoading();
                    wx.showToast({
                        title: '系统错误',
                    });
                    _that.setData({ loading: false });
                }
            })
        },

        switchPayeeChange(e) {
            wx.showLoading();
            wx.request({
                url: `${app.globalData.baseUrl}/api/classify/updateCollection`,
                method: "POST",
                data: { id: e.currentTarget.dataset.id, isDisable: e.detail ? '1' : '0' },
                header: {
                    "Content-Type": "application/json",
                    'token': wx.getStorageSync('token'),
                },
                success: (res) => {
                    wx.hideLoading();
                    if (res.data.code == 0) {
                        wx.showToast({
                            title: res.data.msg || '报错啦！',
                            duration: 2000
                        })
                        this.triggerEvent('addCollectionCommit');
                    } else if (res.data.code == 500) {
                        wx.showToast({
                            title: res.data.msg || '报错啦！',
                            icon: 'error',
                            duration: 2000
                        })
                    } else {
                        wx.showToast({
                            title: '系统错误，请重新登录',
                        })
                    }
                },
                fail: () => {
                    wx.hideLoading();
                    wx.showToast({
                        title: '系统错误',
                    });
                    _that.setData({ loading: false });
                }
            })
        },

        onChangeShow() {
            this.setData({
                isChange: true,
                fluxFee: '',
                flexDes: ''
            });
        },

        onChangeHidden() {
            this.setData({ isChange: false });
        },

        onPayeeShow() {
            this.setData({
                isPayee: true,
                money: '',
                reason: '',
                fees: '',
                receipts: ''
            });
        },

        onPayeeHidden() {
            this.setData({ isPayee: false });
        },

        onFluxChange(e) {
            this.setData({ fluxFee: e.detail });
        },

        onFluxDscChange(e) {
            this.setData({ flexDes: e.detail });
        },

        onMoneyChange(e) {
            this.setData({ money: e.detail });
            if (this.data.fees && e.detail) this.setData({ fees: parseFloat(e.detail - this.data.fees).toFixed(2) })
        },

        onReasonChange(e) {
            this.setData({ reason: e.detail });
        },

        onFeesChange(e) {
            this.setData({ fees: e.detail });
        },

        onReceiptsChange(e) {
            this.setData({ receipts: e.detail });
            if (this.data.money && e.detail) this.setData({ fees: parseFloat(this.data.money - e.detail).toFixed(2) })
        },

        addChange() {
            const { fluxFee, flexDes } = this.data;
            if (!fluxFee) {
                wx.showToast({
                    title: '请填写变动金额',
                    icon: 'error',
                })
                return;
            }
            const reg = /^[\-\+]?\d+(\.\d+)?$/;
            if (!reg.test(fluxFee)) {
                wx.showToast({
                    title: '金额请输入数字',
                    icon: 'error',
                })
                return;
            }
            const _that = this;
            wx.showModal({
                content: '你确认添加变动吗？',
                success: function (sm) {
                    if (sm.confirm) {
                        wx.showLoading();
                        wx.request({
                            url: `${app.globalData.baseUrl}/api/classify/saveChange`,
                            method: "POST",
                            data: {
                                financeId: _that.properties.financeId,
                                money: parseFloat(fluxFee).toFixed(2),
                                reason: flexDes,
                            },
                            header: {
                                "Content-Type": "application/json",
                                'token': wx.getStorageSync('token'),
                            },
                            success: (res) => {
                                wx.hideLoading();
                                if (res.data.code == 0) {
                                    wx.showToast({
                                        title: res.data.msg || '报错啦！',
                                        duration: 2000
                                    })
                                    _that.triggerEvent('addChangeCommit');
                                    _that.onChangeHidden();
                                } else if (res.data.code == 500) {
                                    wx.showToast({
                                        title: res.data.msg || '报错啦！',
                                        icon: 'error',
                                        duration: 2000
                                    })
                                } else {
                                    wx.showToast({
                                        title: '系统错误，请重新登录',
                                    })
                                }
                            },
                            fail: () => {
                                wx.hideLoading();
                                wx.showToast({
                                    title: '系统错误',
                                });
                                _that.setData({ loading: false });
                            }
                        })
                    }
                }
            })
        },

        addPayee() {
            const { money, reason, fees, receipts } = this.data;
            if (!money) {
                wx.showToast({
                    title: '请填写收款金额',
                    icon: 'error',
                })
                return;
            }
            if (!receipts) {
                wx.showToast({
                    title: '请填写实际入账',
                    icon: 'error',
                })
                return;
            }
            const _that = this;
            wx.showModal({
                content: '你确认添加收款吗？',
                success: function (sm) {
                    if (sm.confirm) {
                        wx.showLoading();
                        wx.request({
                            url: `${app.globalData.baseUrl}/api/classify/saveCollection`,
                            method: "POST",
                            data: {
                                financeId: _that.properties.financeId,
                                money: parseFloat(money).toFixed(2),
                                reason,
                                fees,
                                receipts: parseFloat(receipts).toFixed(2)
                            },
                            header: {
                                "Content-Type": "application/json",
                                'token': wx.getStorageSync('token'),
                            },
                            success: (res) => {
                                wx.hideLoading();
                                if (res.data.code == 0) {
                                    wx.showToast({
                                        title: res.data.msg || '报错啦！',
                                        duration: 2000,
                                        icon: 'error',
                                    })
                                    _that.triggerEvent('addCollectionCommit');
                                    _that.onPayeeHidden();
                                } else if (res.data.code == 500) {
                                    wx.showToast({
                                        title: res.data.msg || '报错啦！',
                                        icon: 'error',
                                        duration: 2000
                                    })
                                } else {
                                    wx.showToast({
                                        title: '系统错误，请重新登录',
                                    })
                                }
                            },
                            fail: () => {
                                wx.hideLoading();
                                wx.showToast({
                                    title: '系统错误',
                                });
                                _that.setData({ loading: false });
                            }
                        })
                    }
                }
            })
        },
        deleteRow(e) {
            const id = e.currentTarget.dataset.id;
            const url = e.currentTarget.dataset.url;
            const that = this;
            wx.showModal({
                content: '确认删除吗？',
                success: function (sm) {
                    if (sm.confirm) {
                        wx.request({
                            url: `${app.globalData.baseUrl}${url == '变动' ? '/api/classify/delChangeId?id=' : '/api/classify/delCollectionId?id='}${id}`,
                            method: "GET",
                            header: {
                                'token': wx.getStorageSync('token'),
                                "Content-Type": "application/json"
                            },
                            success: (res) => {
                                if (res.data.code == 0) {
                                    if (url == '变动') that.triggerEvent('addChangeCommit');
                                    else that.triggerEvent('addCollectionCommit');
                                    wx.showToast({
                                        title: '成功删除',
                                    });
                                } else {
                                    wx.showToast({
                                        title: res.data.msg || '报错啦！',
                                        duration: 2000
                                    })
                                }
                            }
                        })
                    } else {
                        console.log('用户点击取消');
                    }
                }
            })
        },

        activeChange(e) {
            this.setData({ active: e.detail });
        },

        collapseChange(e) {
            this.setData({ activeNames: e.detail.slice(-1) });
        },

    }
})